package gmodel

import (
	"context"
	"errors"
	"fmt"

	"gitee.com/851537/gmodel/rds"
	jsoniter "github.com/json-iterator/go"
	"gorm.io/gorm"
)

type BaseRedisHash struct {
	Base
}

func (b *BaseRedisHash) check(child IRedis) (IRedisHash, error) {
	var (
		ok     bool
		child2 IRedisHash
	)
	if child2, ok = child.(IRedisHash); !ok {
		return nil, errors.New("当前对象不是IRedisHash类型")
	}
	return child2, nil
}

func (b *BaseRedisHash) RedisSet(child IRedis) error {
	var (
		child2 IRedisHash
		err    error
	)
	if child2, err = b.check(child); err != nil {
		return err
	} else if jsonstr, err := jsoniter.MarshalToString(child2); err != nil {
		return err
	} else {
		//hlog.Debugf("BaseRedisHash::RedisSet %s-%v:%s", child2.RedisName(), child2.RedisKey(), jsonstr)
		_, err = rds.ClientIns().HSet(context.Background(), child2.RedisName(), fmt.Sprintf("%v", child2.RedisField()), jsonstr).Result()
		return err
	}
}

func (b *BaseRedisHash) RedisGet(child IRedis) error {
	var (
		ok     bool
		child2 IRedisHash
		//err    error
	)
	if child2, ok = child.(IRedisHash); !ok {
		return errors.New("当前对象不是IRedisHash类型")
	}
	if jsonstr := rds.ClientIns().HGet(context.Background(), child.RedisName(), child2.RedisField()).Val(); jsonstr != "" {
		return jsoniter.UnmarshalFromString(jsonstr, child)
		//if err = jsoniter.UnmarshalFromString(jsonstr, child); err != nil {
		//	return nil, err
		//}
	}
	return gorm.ErrRecordNotFound
}

func (b *BaseRedisHash) RedisDel(child IRedis) (err error) {
	var (
		ok     bool
		child2 IRedisHash
	)
	if child2, ok = child.(IRedisHash); !ok {
		return errors.New("当前对象不是IRedisHash类型")
	}
	_, err = rds.ClientIns().HDel(context.Background(), child.RedisName(), child2.RedisField()).Result()
	return
}

func (b *BaseRedisHash) HGetAll(child IRedis) (m map[string]string, err error) {
	return rds.ClientIns().HGetAll(context.Background(), child.RedisName()).Result()
}

func (b *BaseRedisHash) HMGet(child IRedis, keys ...string) ([]interface{}, error) {
	return rds.ClientIns().HMGet(context.Background(), child.RedisName(), keys...).Result()
}

func (b *BaseRedisHash) HScan(child IRedis, cursor uint64, match string, count int64) (keys []string, nextCursor uint64) {
	return rds.ClientIns().HScan(context.Background(), child.RedisName(), cursor, match, count).Val()
}

//func (b *BaseRedisHash) HScanV2(child IRedis, cursor uint64, match string, count int64) (data map[string]interface{}, nextCursor uint64) {
//	var (
//		keys []string
//		key  string
//	)
//	data = make(map[string]interface{})
//	keys, nextCursor = this.HScan(child, cursor, match, count)
//	//logs.Debug("keys",keys)
//	//logs.Debug("cursor",cursor)
//	//time.Sleep(10*time.Second)
//	for k, v := range keys {
//		if k%2 == 0 {
//			key = v
//		} else {
//			data[key] = v
//		}
//	}
//	return
//}

//func (b *BaseRedisHash) HKeys(child IRedis) []string {
//	return NewRedisDao().HKeys(child.RedisName()).Val()
//}
//
//func (b *BaseRedisHash) HLen(child IRedis) int64 {
//	return NewRedisDao().HLen(child.RedisName()).Val()
//}
//
//func (b *BaseRedisHash) HExists(child IRedisHash) bool {
//	return NewRedisDao().HExists(child.RedisName(), fmt.Sprintf("%v", child.RedisKey())).Val()
//}
